/* Yet Another Experimental Software Renderer
 *
 * Copyright (c) 2010 Pawel "Hery" Dziepak
 */

#ifndef CHAR_READER_H_
#define CHAR_READER_H_

#include <string>

namespace yasl {
	class char_reader {
	public:
		virtual char get() const = 0;
		virtual bool next() = 0;
	};

	class file_reader : public char_reader {
	public:
		file_reader(const std::string &name) : position(0) {
			FILE *fp = NULL;
			fopen_s(&fp, name.c_str(), "rb");

			fseek(fp, 0, SEEK_END);
			size = ftell(fp);
			fseek(fp, 0, SEEK_SET);

			data = new char[size];
			fread(data, size, 1, fp);

			fclose(fp);
		}

		char get() const {
			if (position >= size)
				return 0;

			return data[position];
		}

		bool next() {
			position++;

			return position < size;
		}
	private:
		char *data;

		long position;
		long size;
	};
}

#endif